package de.htwds.mada.todolistDB;

import java.sql.SQLException;
import java.util.List;
import java.util.ListIterator;

import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import de.htwds.mada.R;
import de.htwds.mada.todolistDB.db.DatabaseHelper;
import de.htwds.mada.todolistDB.db.Repository;
import de.htwds.mada.todolistDB.model.Todo;

public class TODODBListActivity extends ListActivity{

//--------------------- Felder ----------------------------------------------------//
	protected static Repository db;
	protected static MadaArrayAdapter<Todo> adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		if(adapter == null)
			adapter = new MadaArrayAdapter<Todo>(this);
		
		setListAdapter(adapter);
		
		if(db == null) // Objekt fuer Datenbank-Zugriff anlegen
			db		= new Repository(new DatabaseHelper(this));
		
		dbZugriff();
        
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.db_menu_todo_main, menu); // Hier noch aendern
		return super.onCreateOptionsMenu(menu);
	}

	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent myIntent;
		switch (item.getItemId()){
			case R.id.newToDoDB:
				myIntent = new Intent(TODODBListActivity.this, TODODBDetailsActivity.class);
				TODODBListActivity.this.startActivity(myIntent);
				break;
			case R.id.settingsToDoDB:
				myIntent = new Intent(TODODBListActivity.this, TODODBSettingsActivity.class);
				TODODBListActivity.this.startActivity(myIntent);
				break;
			case R.id.newPriorityDB:
				myIntent = new Intent(TODODBListActivity.this, TODODBNewPriorityActivity.class);
				TODODBListActivity.this.startActivity(myIntent);
				break;
			case R.id.newCategoryDB:
				myIntent = new Intent(TODODBListActivity.this, TODODBNewCategorieActivity.class);
				TODODBListActivity.this.startActivity(myIntent);
				break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Intent myIntent = new Intent(TODODBListActivity.this, TODODBDetailsActivity.class);
		myIntent.putExtra("object", (Todo) l.getAdapter().getItem(position));
		TODODBListActivity.this.startActivity(myIntent);
		super.onListItemClick(l, v, position, id);
	}
	
	
	private void changeTextsize(){
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
		adapter.setTextSize(Float.valueOf((prefs.getString("tododbTextSize", "16"))), TypedValue.COMPLEX_UNIT_SP);
		adapter.notifyDataSetChanged();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		dbZugriff();
		changeTextsize();
	}
	
	/**
	 * Methode, die den Zugriff auf die Datenbank veranlasst
	 */
	private void dbZugriff(){
		adapter.clear();
		
		List<Todo> list = null;
		try {
			list = db.getTodos(); //Datenbank-Zugriff lesen
		} catch (SQLException e) {
			e.printStackTrace();
			Toast.makeText(this, R.string.error_db_write, Toast.LENGTH_SHORT).show();
		}
		
		//Ergebnisse werden durchiteriert und in die Liste eingetragen
		ListIterator<Todo> iterator = list.listIterator();
		while(iterator.hasNext()){
			Todo test = iterator.next();
			adapter.add(test);
		}
		
		adapter.notifyDataSetChanged();
	}
}
